package week_eight;

public class ReverseOnlyLetters_917 {
    public String reverseOnlyLetters(String s) {
        char[] chars = s.toCharArray();
        int n = chars.length;
        int l = 0, r = n - 1;
        while (l < r) {
            while (l < r && !isLetter(chars[l])) l++;
            while (l < r && !isLetter(chars[r])) r--;
            char tmp = chars[l];
            chars[l] = chars[r];
            chars[r] = tmp;
            r--;
            l++;
        }

        return String.valueOf(chars);

    }

    private boolean isLetter(char c) {
        return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
    }
}
